package com.qxsx.midi.activity.exam;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.activity.personal.UserData;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import static android.view.View.*;

/**
 * Created by Zrj on 2015/9/7.
 */
public class ClassVideoDetailsActivity extends BaseActivity implements OnClickListener {
    private CustomProgress customProgress;

    //
    private ImageView iv_start, iv_beijing;
    private String id;
    private TextView tv_name, tv_content;
    private String lesson_name, lesson_brief, lesson_url, lesson_covert;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exam_classvideodetails);
        setTitle("等级1课程视频");
        setNavBtn(R.mipmap.back, 0);

        Intent intent = getIntent();
        id = intent.getStringExtra("id");//得到ID
        findById();
        classvideodetails();
    }

    private void findById() {
        iv_start = (ImageView) findViewById(R.id.iv_start);
        iv_start.setOnClickListener(this);
        iv_beijing = (ImageView) findViewById(R.id.iv_beijing);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_content = (TextView) findViewById(R.id.tv_content);


    }

    //公开课详情
    private void classvideodetails() {
        customProgress = CustomProgress.show(ClassVideoDetailsActivity.this, "请稍后....", true, null);

        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        try {
            object.put("lession_id", id);
            array.put(object);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("512", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                if (array == null) {
                    return;
                }
                try {
                    JSONObject object1 = array.getJSONObject(0);
                    lesson_name = object1.getString("lesson_name");
                    lesson_brief = object1.getString("lesson_brief");
                    lesson_url = object1.getString("lesson_url");
                    lesson_covert = object1.getString("lesson_covert");
                    Log.w("url", lesson_covert);
                    MyNet.setNetImage(ClassVideoDetailsActivity.this, iv_beijing, lesson_covert);
                    tv_name.setText(lesson_name);
                    tv_content.setText(lesson_brief);


                } catch (JSONException e) {
                    e.printStackTrace();
                }
                customProgress.dismiss();
            }
        });


    }

    /**
     * @param component
     * @param v
     */
    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {

        switch (component) {
            case LEFT:
                finish();
                break;

        }
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_start:
                //跳转到商家详情页面
                if (TextUtils.isEmpty(lesson_url)) {
                    return;
                }
                Intent it = new Intent(ClassVideoDetailsActivity.this, VideoPlayActivity.class);
                it.putExtra("lesson_url", lesson_url);//视频地址
                startActivity(it);

                // startActivity(new Intent(ClassVideoDetailsActivity.this, VideoPlayActivity.class));


                break;

        }
    }
}
